package com.maixy.studyonlineapi.controller;

import com.maixy.studyonlineapi.apis.ApiController;
import com.maixy.studyonlineapi.apis.ApiRequest;
import com.maixy.studyonlineapi.apis.ApiResponse;
import com.maixy.studyonlineapi.apis.Privilege;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.exceptions.TipException;
import com.maixy.studyonlineapi.result.PageData;
import com.maixy.studyonlineapi.service.AuthService;
import com.maixy.studyonlineapi.utils.DataTypeUtil;
import com.maixy.studyonlineapi.view.AuthView;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Classname AuthController
 * @Description TODO
 * @Date 2020/11/15 19:05
 * @Author by maixy
 */
@RestController
@RequestMapping("/v1/auth")
public class AuthController extends ApiController {

    @Resource
    private AuthService authService;

    /**
     * 判断某个人是否加入了活动
     * @param request --activityId 活动id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse hasAuth(ApiRequest request) {
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");

        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        Boolean hasAuth = authService.hasAuth(teamId, userId, activityId);
        return ApiResponse.ok().addValueToData("hasAuth", hasAuth);
    }

    /**
     * 加入班级
     * @param request --activityId 活动id
     * @return com.maixy.studyonlineapi.apis.ApiResponse
     */
    @Privilege
    public ApiResponse authActivity(ApiRequest request) {
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");

        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        authService.authActivity(teamId, userId, activityId);
        return ApiResponse.ok();
    }

    /**
     * 退出班级
     *
     * @param request -- activityId
     * @return --
     */
    @Privilege
    public ApiResponse kickOut(ApiRequest request) {
        Long currentUserId = request.getDataParamAsLong("currentUserId");
        Long activityId = request.getDataParamAsLong("activityId");
        Long teamId = DataTypeUtil.getLong(request.getTeamId());
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());


        if (null == activityId || null == currentUserId) {
            throw new TipException("参数缺失");
        }
        authService.kickOut(teamId, userId, activityId, currentUserId);
        return ApiResponse.ok();
    }

    /**
     * 退出班级
     *
     * @param request -- activityId 活动id
     * @return --
     */
    @Privilege
    public ApiResponse quit(ApiRequest request) {
        Long userId = DataTypeUtil.getLong(request.getUser().getUserId());
        Long activityId = request.getDataParamAsLong("activityId");

        if (null == activityId) {
            throw new TipException("参数缺失");
        }
        authService.quit(userId, activityId);
        return ApiResponse.ok();
    }

    /**
     * 课程已报名列表
     *
     * @param request -- activityId 活动id
     * @return --
     */
    @Privilege
    public ApiResponse getAuthList(ApiRequest request) {
        Long activityId = request.getDataParamAsLong("activityId");
        Page page = Page.validation(request.getPage());
        if (null == activityId) {
            throw new TipException("参数缺失");
        }

        PageData<AuthView> pageData = authService.getAuthList(activityId, page);
        return ApiResponse
                .ok()
                .addValueToData("pageData", pageData);
    }


}
